﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using LEDContract;
using LEDHelper;
namespace LED.Show
{
    #region========Readme=================
    /*
     1.首先用C#实现所有的逻辑
     2.必要时，再用C++调用C#的DLL，包装成WIN32的程序
     3.所有的输出文字，都经过C#转成图片，包装到MemDc再输出
     4.为每个屏幕建立一个档案。在界面上模拟这个屏幕
        4.1 建立屏幕数据库
        4.2 读入屏幕列表
        4.2.1 如果没有，则新建一个数据集
        4.2.2 对于新建记录，进行保存
        4.3 根据屏幕列表，显示屏幕数据
        4.4 建立一个模拟显示区域
        4.5 对显示区域建立方格Grid
        4.6 每个Grid是一个控制的区域，建立一个Rect
        4.7 将Rect的内容发送给一个帧
        4.8 多个帧
     5.屏幕上有一个区域专门用于模拟屏幕的显示。
     6.节目区分多个页面
     7.每个页面有持续时间
     8.每个页面能够划分区域
     9.每个区域都有自己的一个MemDCID，都是一个图片
     10.文字的先转成图片，再增加动作。
     11.文字的复杂效果，通过C#实现
     */
    #endregion

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        int nLEDID = 1;
        Int32 dev = -1;
        WXLED wx = new WXLED();
        private HwndSource m_hwndSource;
        public IntPtr Hwnd;
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);

        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //int deviceid = System.Convert.ToInt16(txtDeviceID.Text.ToString());
            //dev = WXLED.LED_GetBright(deviceid);
            wx.InsigmaLEDDriver_Init();
            PictureFlow();
        }
        #region=======Confirmed===========
        private void cmdInsigmaLEDDriver_Init_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                wx.InsigmaLEDDriver_Init();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace, "Error");
            }
        }

        private void cmdInsigmaLEDDriver_Cleanup_Click(object sender, RoutedEventArgs e)
        {

        }
        private void cmdLED_IsDeviceOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int deviceid = System.Convert.ToInt16(txtDeviceID.Text.ToString());
                dev = wx.LED_IsDeviceOK(deviceid);
                if (dev == -1)
                {
                    MessageBox.Show("未能初始化任何设备");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace, "Error");
            }
        }

        #endregion
        #region========Device============
        private void cmdSwitchPower_Click(object sender, RoutedEventArgs e)
        {
            int nLEDID = 0;
            int nTurnOn = System.Convert.ToInt16(txtPower.Text.ToString());
            try
            {
                wx.LED_SwitchPower(nLEDID, nTurnOn);
            }
            catch (Exception ex)
            {
            }
        }

        private void IsDeviceTurnOn_Click(object sender, RoutedEventArgs e)
        {
            int nLEDID = 0;
            try
            {
                wx.LED_IsDeviceTurnOn(nLEDID);
            }
            catch (Exception ex)
            {
            }

        }

        private void cmdRestart_Click(object sender, RoutedEventArgs e)
        {
            int nLEDID = 0;

            try
            {
                wx.LED_Restart(nLEDID);
            }
            catch (Exception ex)
            {
            }
        }

        private void cmdGetBright_Click(object sender, RoutedEventArgs e)
        {

            int nLEDID = 0;
            Int32 lValue = 6;
            try
            {
                lValue = wx.LED_GetBright(nLEDID);
            }
            catch (Exception ex)
            {
            }
        }

        private void cmdSetBright_Click(object sender, RoutedEventArgs e)
        {
            int nLEDID = 0;
            Int32 lValue = System.Convert.ToInt32(txtBright.Text.ToString());
            try
            {
                wx.LED_SetBright(nLEDID, lValue);
            }
            catch (Exception ex)
            {
            }
        }

        private void cmdAdjustCurrentTime_Click(object sender, RoutedEventArgs e)
        {
            int nLEDID = 0;
            Int32 lValue = 6;
            try
            {
                wx.LED_AdjustCurrentTime(nLEDID);
            }
            catch (Exception ex)
            {
            }
        }
        #endregion
        #region========Edit=================


        #endregion
        #region=========Program================
        private void PictureFlow()
        {
            try
            {
                wx.LED_LEDScreenInit(nLEDID, LEDAPI.DEVICE_TYPE_UDP, 238, 32);
                wx.LED_BeginProgram(nLEDID, 1000, "");
                AddTextBlock_Click(null, null);
                //wx.LED_BeginProgram(nLEDID, 1000, "");
                //AddBmpBlock_Click(null, null);
                wx.LED_EndProgram(nLEDID);
                cmdSendToScreen_Click(null, null);
                wx.LED_GetBright(nLEDID);
            }
            catch (Exception ex)
            {
            }
        } 
        private void cmdLEDScreenInit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Int32 nLEDID = 1;
                Int32 nScreenType = 1;
                Int32 nWidth = 128;
                Int32 nHeight = 32;
                wx.LED_LEDScreenInit(nLEDID, nScreenType, nWidth, nHeight);
            }
            catch (Exception ex)
            {
            }
        }
        private void BeginProgram_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Int32 nLEDID = 1;
                Int32 nProgramID = System.Convert.ToInt32(txtFrameID.Text.ToString());
                Int32 IDisplayTime = System.Convert.ToInt32(txtDisplayTime.Text.ToString());
                string szBackBMP = @"D:\SXDFile\A1.jpg";
                wx.LED_BeginProgram(nLEDID, IDisplayTime, szBackBMP);
            }
            catch (Exception ex)
            {

            }

        }
        private void AddTextBlock_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                txtString.Text = "佛说宇宙人生真相 大道本源西方金  宇宙大母 南无大日如来";
                string szText = "大道本源西方金";
                LEDBlock rect = new LEDBlock();
                rect.nStartX = 0;
                rect.nStartY = 0;
                rect.nWidth = 128;
                rect.nHeight = 32;
                FontDescriptor lpFont = new FontDescriptor();
                lpFont.szFontName = "楷体";
                lpFont.lFontSize = 18;
                lpFont.lFontColor = 234;
                lpFont.nBold = 1;
                lpFont.nUnderline = 1;
                lpFont.nStrick = 1;
                int nEffectType = 2;
                int nSpeed = 2;
                Int32 lTrasnparent = 1;
                wx.LED_AddTextBlock(nLEDID, szText, rect, ref lpFont, nEffectType, nSpeed, lTrasnparent);
            }
            catch (Exception ex)
            {
            }
        }
        private void AddClockBlockClick(object sender, RoutedEventArgs e)
        {
            try
            {
                ClockDescriptor lpClock = new ClockDescriptor();

                LEDBlock rect = new LEDBlock();
                rect.nStartX = 0;
                rect.nStartY = 0;
                rect.nWidth = 128;
                rect.nHeight = 32;
                FontDescriptor lpFont = new FontDescriptor();
                //lpFont.szFontName = "楷书".ToCharArray;
                lpFont.lFontSize = 14;
                lpFont.lFontColor = 234;
                lpFont.nBold = 1;
                lpFont.nUnderline = 1;
                lpFont.nStrick = 1;
                int nEffectType = 2;
                int nSpeed = 2;
                Int32 lTrasnparent = 1;
                wx.LED_AddClockBlock(nLEDID, rect, ref lpClock,ref lpFont, nEffectType, nSpeed, lTrasnparent);

            }
            catch (Exception ex)
            {
            }
        }
        private void AddBmpBlock_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int nLEDID = 1;
                string szBmpFile = txtBMP.Text.ToString();
                LEDBlock destRect = new LEDBlock();
                destRect.nHeight = 32;
                destRect.nWidth = 238;
                  destRect.nStartX=0;
                  destRect.nStartY = 0;

                LEDBlock sourceRect = new LEDBlock();
                sourceRect.nHeight = 32;
                sourceRect.nWidth = 238;
                sourceRect.nStartX = 0;
                sourceRect.nStartY = 0;
                int nEffectType = 1;
                int nSpeed = 1;
                Int32 lTrasnparent = 1;
                wx.LED_AddBmpBlock(nLEDID, szBmpFile, destRect, sourceRect, nEffectType, nSpeed, lTrasnparent);

            }
            catch (Exception ex)
            {

            }
        }
        private void cmdLEDClear_Click(object sender, RoutedEventArgs e)
        {

        }
        #endregion
        #region=========Flow================
      
        private void Flow1()
        {
            /*
            #region 初始化设备 
            #endregion
            #region 增加一个节目
            MyLED.MakeRoot(LEDHelper.ROOT_PLAY, LEDHelper.SCREEN_COLOR);
            #endregion
            #region 增加一个帧
            MyLED.AddLeaf(1000);
            #endregion
            #region 划定一个区域
            SRECT srect = new SRECT();
            MyLED.SetRect(ref srect, 0, 0, 280, 32);
            #endregion
            #region 增加一个图片
            IntPtr screenDC = MyLED.GetDC(IntPtr.Zero);
            IntPtr memDC = MyLED.CreateCompatibleDC(screenDC);
            Bitmap bmp = new Bitmap(@"D:\SXDFile\A3.jpg");
            IntPtr hBitmap = bmp.GetHbitmap();
            hBitmap = bmp.GetHbitmap();
            MyLED.SelectObject(memDC, hBitmap); // Select bitmap from compatible bitmap to memDC 
            MyLED.AddWindow(memDC, 280, 32, ref srect, 1, 1, 1);
            MyLED.DeleteObject(hBitmap);
            MyLED.ReleaseDC(IntPtr.Zero, screenDC);
            MyLED.ReleaseDC(IntPtr.Zero, memDC);
            #endregion
            //ReleaseDC(hdc, IntPtr.Zero);
            #region 发送到设备
            MyLED.LED_SendToScreen(dev, 1, "192.168.0.101", 6666);  //发送到1组1号显示屏，此处需要根据现场情况修改
            #endregion
            */
        }
        private void Flow2()
        {
    
            wx.LED_LEDScreenInit(1, LEDAPI.DEVICE_TYPE_UDP, 238, 32);

            #region 增加一个图片
            #region 增加一个帧
             wx.LED_BeginProgram(1, 1000, "");
            #endregion
            #region 划定一个区域 
            #endregion
            AddBmpBlock_Click(null, null); 
            #endregion
            #region 增加一个文字
            #region 增加一个帧
            wx.LED_BeginProgram(1, 1000, "");
            #endregion
            #region 划定一个区域

            #endregion
            AddTextBlock_Click(null, null);
            #endregion

            #region 增加一个时钟
            #region 增加一个帧
             wx.LED_BeginProgram(1, 1000, "");
            #endregion
            #region 划定一个区域
            #endregion
            AddClockBlockClick(null, null);
            #endregion
            //ReleaseDC(hdc, IntPtr.Zero);
            #region 发送到设备
            cmdSendToScreen_Click(null, null);
            #endregion
         
        }
        #endregion
        private void cmdPreUploadBMP_Click(object sender, RoutedEventArgs e)
        {
            int nLEDID = 0;
            string szBmpFile = txtBMP.Text.ToString();
            try
            {
                wx.LED_PreUploadBMP(nLEDID, szBmpFile);
            }
            catch (Exception ex)
            {

            }
        }

        private void cmdRemoveUploadedBmp_Click(object sender, RoutedEventArgs e)
        {
            int nLEDID = 0;
            string szBmpFile = txtszBmpFile.Text.ToString();
            try
            {
                wx.LED_RemoveUploadedBmp(nLEDID, szBmpFile);
            }
            catch (Exception ex)
            {

            }
        }

        private void cmdGetFreeUploadBmpSpace_Click(object sender, RoutedEventArgs e)
        {
            int nLEDID = 1;
            string szBmpFile = txtszBmpFile.Text.ToString();
            try
            {
                wx.LED_GetFreeUploadBmpSpace(nLEDID);
            }
            catch (Exception ex)
            {

            }
        }


        private void cmdAddLine_Click(object sender, RoutedEventArgs e)
        {

        }

        private void cmdAddAnimationBlock_Click(object sender, RoutedEventArgs e)
        {

        }

        private void cmdAddVideoBlock_Click(object sender, RoutedEventArgs e)
        {

        }

        private void cmdEndProgram_Click(object sender, RoutedEventArgs e)
        {

        }

        private void cmdSendToScreen_Click(object sender, RoutedEventArgs e)
        {
            wx.LED_SendToScreen(nLEDID);
        }

        private void cmdFastenProgram_Click(object sender, RoutedEventArgs e)
        {

        }

        private void cmdRunProgram_Click(object sender, RoutedEventArgs e)
        {

        }

        private void cmdDeleteProgram_Click(object sender, RoutedEventArgs e)
        {

        }

        private void cmdLEDDef_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {

            }

        }

        private void cmdBmpTest_Click(object sender, RoutedEventArgs e)
        {
            PictureFlow();
        }

        private void cmdTextTest_Click(object sender, RoutedEventArgs e)
        {

        }

        private void cmdInterfaceTest_Click(object sender, RoutedEventArgs e)
        {
            Flow2();
        }
    }
}
